package ModelLayer;
import java.util.ArrayList;


/**
 * Write a description of class OrderContainer here.
 * 
 * @author Group1 
 * @version 2011-06-08
 */
public class OrderContainer
{
    // instance variables - replace the example below with your own
    private ArrayList<Order> order;
    private static OrderContainer _instance = null;
    private int lastCreatedOrderId;
    /**
     * Constructor for objects of class OrderContainer
     */
    private OrderContainer()
    {
       order = new ArrayList<Order>();
       lastCreatedOrderId = 0;
    }
    
    /**
     * Creates an instance of OrderContainer
     * @return Returns an instance of OrderContainer
     */
    public static OrderContainer getInstance()
    {
        if(_instance == null)
        {
            _instance = new OrderContainer();
        }
        return _instance;
    }
    
    /**
     * create a order to OrderContainer
     * @param take a parameter of type order
     */    
    public void createOrder(Order neworder)
    {
        order.add(neworder);
    }
    
    /**
     * Find a order from the OrderContainer
     * @param name Name of the order
     */    
    public Order findOrder(int id)
    {
        boolean found = false;
        int index = 0;
        Order o = null;
        while (index < order.size() && !found)
        {
            if (order.get(index).getId() == id)
            {
                found = true;
                o = order.get(index);
            }
            index++;
        }

        return o;
    }
    
    public ArrayList<Order> findOrderByName(String searchName)
    {
        int index = 0;
        ArrayList<Order> customerOrders = new ArrayList<Order>(); 
        while (index < order.size())
        {
            if(order.get(index).getCustomersName().equals(searchName))
            {
               customerOrders.add(order.get(index));
            }
            index++;
        }
        return customerOrders;
    }
    
    /**
     * Delete order from the OrderContainer
     * @param take an objekt of the type order
     */    
    public void deleteOrder(int id)
    {
        boolean found = false;
        int index = 0;
        while (index < order.size() && !found)
        {
            if (order.get(index).getId() == id)
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        order.get(index).setStatus("canceled");
    }

    /**
     * Lists all Order in the OrderContainer 
     */    
    public ArrayList<Order> listAllOrder()
    {
        return order;
    }
    
    public void setLastCreatedOrderId()
    {
        lastCreatedOrderId = order.size() + 1;
    }
    
    public int getLastCreatedOrderId()
    {
        return lastCreatedOrderId;
    }
    
}
